﻿using BouncingBallGame.Common;
using BouncingBallGame.DataModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BouncingBallGame.Models;
using System.Windows.Input;
using BouncingBallGame.Helpers;
using Windows.UI.Popups;

namespace BouncingBallGame.ViewModels
{
    public class HomePageViewModel : BindableBase
    {
        #region Fields

        private int selectedIndex;
        private List<GameLevelModel> models;
        private PageManager pageManager = new PageManager();
        private ICommand helpCommand;
        private const string helpInfo = @"Bouncing ball game is created by Deyan Yosifov, student in Telerik Academy.
The aim of the game is to throw the red ball by dragging it and to select all yellow gifts in the level with as minimum touches of the ball as possible.
You may select one of the levels from the list or create your own custom level. Look at the app bars of any page in order to see possible commands.
When you have chosen the level, press the big button with level's description in order to start the game.
Enjoy! :)";

        #endregion


        #region Properties

        public ObservableCollection<PlayGroundVM> LevelsFull { get; set; }
        public ObservableCollection<PlayGroundVM> LevelsSnapped { get; set; }
        public ObservableCollection<PlayGroundVM> LevelsInfo { get; set; }
        public PlayGroundVM EmptyPlayGround = new PlayGroundVM() { Name = "Select level to play" };

        public int SelectedIndex
        {
            get
            {
                return this.selectedIndex;
            }
            set
            {
                if (this.selectedIndex != value)
                {
                    this.selectedIndex = value;
                    this.OnPropertyChanged("SelectedIndex");
                    this.OnSelectedIndexChanged();
                }
            }
        }

        public PlayGroundVM BigLevelsInfo
        {
            get
            {
                if (this.SelectedIndex >= 0 && this.LevelsInfo.Count > this.SelectedIndex)
                {
                    return this.LevelsInfo.ElementAt(this.SelectedIndex);
                }
                else
                {
                    return this.EmptyPlayGround;
                }
            }
        }

        public PlayGroundVM SmallLevelsInfo
        {
            get
            {
                if (this.SelectedIndex >= 0)
                {
                    return this.LevelsFull.ElementAt(this.SelectedIndex);
                }
                else
                {
                    return this.EmptyPlayGround;
                }
            }
        }

        public PageManager PageManager
        {
            get
            {
                return this.pageManager;
            }
        }

        public GameLevelModel SelectedLevel
        {
            get
            {
                return this.BigLevelsInfo.GameLevel;
            }
        }

        public ICommand Help
        {
            get
            {
                if (this.helpCommand == null)
                {
                    this.helpCommand = new RelayCommand(this.HandleHelpCommand);
                }

                return this.helpCommand;
            }
        }

        #endregion


        #region Constructors

        public HomePageViewModel()
        {
            this.LevelsFull = new ObservableCollection<PlayGroundVM>();
            this.LevelsSnapped = new ObservableCollection<PlayGroundVM>();
            this.LevelsInfo = new ObservableCollection<PlayGroundVM>();

            this.models = new LevelsManager().Levels;

            foreach (var model in models)
            {
                PlayGroundVM playground = new PlayGroundVM() { GameLevel = model };
                playground.FitInSize(500, 130);
                this.LevelsFull.Add(playground);

                playground = new PlayGroundVM() { GameLevel = model };
                playground.FitInSize(480, 400);
                this.LevelsInfo.Add(playground);

                playground = new PlayGroundVM() { GameLevel = model };
                playground.FitInSize(80, 80);
                this.LevelsSnapped.Add(playground);
            }
        }

        #endregion


        #region Methods
        
        private void OnSelectedIndexChanged()
        {
            this.OnPropertyChanged("BigLevelsInfo");
            this.OnPropertyChanged("SmallLevelsInfo");
            this.OnPropertyChanged("SelectedLevel");
        }

        private async void HandleHelpCommand(object parameter)
        {
            await new MessageDialog(HomePageViewModel.helpInfo).ShowAsync();
        }

        #endregion
    }
}
